import net.java.games.input.*;

public class GuitarController{
	
	private boolean wham = true;
	
	private ControllerEnvironment ce;
	private Controller[] controllers;
	private Controller controller;
	private Component[] comps;
	
	private final int FORWARD = 15;
	private final int BACKWARD = 15;
	private final int STRUM_BAR = 15;
	private final int GREEN = 5;
	private final int RED = 6;
	private final int YELLOW = 8;
	private final int BLUE = 7;
	private final int ORANGE = 9;
	private final int TOGGLE = 4;
	private final int START = 12;
	private final int BACK = 11;
	private final int WAMMY_BAR = 3;
	private final int STAR_POWER = 2;
	
	private final int WAMMY_BAR_RANGE = 63;
	
	public GuitarController(){
		
		this.ce = ControllerEnvironment.getDefaultEnvironment();
		this.controllers = ce.getControllers();
		this.controller = findController();
		this.comps = controller.getComponents();
		
	}
	
	private Controller findController() {
		Controller temp = null;
		for(int i = 0; i < controllers.length; i++){
			if(controllers[i].toString().equals("Controller (Harmonix Guitar for Xbox 360)") ||
				controllers[i].toString().equals("Harmonix Guitar for Xbox 360 (Controller)"))
			{
				temp = controllers[i];
			}
		}
		if(temp == null){
			System.exit(1);
		}
		
		return temp;
	}
	
	public void poll(){
		controller.poll();
	}
	
	public boolean starPower(){
		return ((int)(comps[STAR_POWER]).getPollData() != 0);
	}
	
	public boolean keyForward(){
		return ((comps[FORWARD]).getPollData() == 1);
	}
	
	public boolean keyBackward(){
		return ((comps[BACKWARD]).getPollData() == 0.5);
	}
	
	public boolean strum(){
		return ((comps[15]).getPollData() == 0.75 || (comps[STRUM_BAR]).getPollData() == 0.25 );
	}
	
	public boolean [] upDownStrum(){
		
		boolean [] temp = {false,false,false};
		float value = comps[STRUM_BAR].getPollData();
		if(value == 0){
			temp[0] = true;
		}else if(value == .75){
			System.out.println(value);
			temp[1] = true;
		}else{
			temp[2] = true;
		}
		return temp;
	}
	
	public int wammyBar() throws InterruptedException
	{	
		int temp = (int)((comps[WAMMY_BAR].getPollData()*(WAMMY_BAR_RANGE/2))) + (WAMMY_BAR_RANGE/2);
		if(wham){
			if(temp != (WAMMY_BAR_RANGE/2)){
				wham = false;
			}
			temp = 0;
		}	
		Thread.sleep(1);
		return temp;
	}
	
	public int toggle(){
		return (int)((comps[TOGGLE].getPollData()*10))/2;
	}
	
	public boolean start(){
		return ((comps[START].getPollData()) != 0.0f);
	}
	
	public boolean back(){
		return ((comps[BACK].getPollData()) != 0.0f);
	}
	
	public boolean green(){
		return ((comps[GREEN].getPollData()) != 0.0f);
	}
	
	public boolean red(){
		return ((comps[RED].getPollData()) != 0.0f);
	}
	
	public boolean yellow(){
		return ((comps[YELLOW].getPollData()) != 0.0f);
	}
	
	public boolean blue(){
		return ((comps[BLUE].getPollData()) != 0.0f);
	}
	
	public boolean orange(){
		return ((comps[ORANGE].getPollData()) != 0.0f);
	}
	
}